#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=510;
int aa[N],c[N],x[4],ans,X,K,k;
struct {
    int id,a;
}a[N],b[N];
char s[N][N];
void solve(int dep,int n){
    if (n==1) X=a[n].id;
    int de=K-k+2-dep;
    for (int i=1;i<=n/2;i++){
        if (s[de][i]=='0') {
            if (a[i*2-1].a>=dep) a[i]=a[i*2-1];
            else a[i]=a[i*2];
        }
        else {
            if (a[i*2].a>=dep) a[i]=a[i*2];
            else a[i]=a[i*2-1];
        }
    }
    solve(dep+1,n/2);
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int n,m,T;
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        cin>>aa[i];
    }
    for (int i=1;i<=m;i++){
        cin>>c[i];
    }
    for (int i=1;;i++){
        if (n<=pow(2,i)){
            K=i;
            break;
        }
    }
    for (int i=1;i<=K;i++){
        for (int j=1;j<=pow(2,K-i);j++){
            cin>>s[i][j];
        }
    }
    cin>>T;
    while (T--){
        cin>>x[0]>>x[1]>>x[2]>>x[3];
        for (int i=1;i<=n;i++){
            a[i].a=aa[i] ^ x[i%4];
            a[i].id=i;
            b[i]=a[i];
        }
        ans=0;
        for (int i=1;i<=m;i++){
            for (int j=1;;j++){
                if (pow(2,j)==c[i]) {
                    k=j;
                    break;
                }
            }
            solve(1,c[i]);
            ans=ans ^ (i*X);
        }
        cout<<ans<<endl;
    }
    return 0;
}
//0pts